package com.lyj.friend.service.exam.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.lyj.common.core.constants.Constants;
import com.lyj.common.core.domain.TableDataInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lyj.common.core.utils.ThreadLocalUtil;
import com.lyj.friend.domain.exam.dto.ExamQueryDTO;
import com.lyj.friend.domain.exam.vo.ExamVO;
import com.lyj.friend.manager.ExamCacheManager;
import com.lyj.friend.mapper.exam.ExamMapper;
import com.lyj.friend.service.exam.ExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LIUYANJIAO
 * Date: 2025-09-05
 * Time: 21:35
 */
@Service
public class ExamServiceImpl implements ExamService {
    @Autowired
    private ExamMapper examMapper;

    @Autowired
    private ExamCacheManager examCacheManager;
    @Override
    public TableDataInfo redisList(ExamQueryDTO examQueryDTO) {
        //从redis中获得 竞赛列表的数据
        Long total = examCacheManager.getListSize(examQueryDTO.getType(),null);
        List<ExamVO> examVOList;
        if(total == null || total<=0 ){
            //redis中没有数据
            examVOList = list(examQueryDTO);
            //拿到数据之后添加都redis中
            examCacheManager.refreshCache(examQueryDTO.getType(),null);
            total = new PageInfo<>(examVOList).getTotal();
        }else {
            //已有数据
            examVOList = examCacheManager.getExamVOList(examQueryDTO,null);
            total = examCacheManager.getListSize(examQueryDTO.getType(),null);
        }
        if(CollectionUtil.isEmpty(examVOList)){
            return TableDataInfo.empty();
        }
        assembleExamVOList(examVOList);
        return TableDataInfo.success(examVOList,total);
    }

    @Override
    public String getFirstQuestion(Long examId) {
        //保证redis中存在 list
        checkAndRefresh(examId);
        return examCacheManager.getFirstQuestion(examId).toString();
    }

    private void checkAndRefresh(Long examId) {
        Long listSize = examCacheManager.getExamQuestionListSize(examId);
        if(listSize == null || listSize<=0){
            examCacheManager.refreshExamQuestionCache(examId);
        }
    }

    @Override
    public String preQuestion(Long examId, Long questionId) {
        checkAndRefresh(examId);
        return examCacheManager.preQuestion(examId,questionId).toString();
    }

    @Override
    public String nextQuestion(Long examId, Long questionId) {
        checkAndRefresh(examId);
        return examCacheManager.nextQuestion(examId,questionId).toString();
    }

    private void assembleExamVOList(List<ExamVO> examVOList) {
        Long userId = ThreadLocalUtil.get(Constants.USER_ID,Long.class);
       //TODO
    }

    private List<ExamVO> list(ExamQueryDTO examQueryDTO){
        //直接从数据库中拿取数据
        PageHelper.startPage(examQueryDTO.getPageNum(),examQueryDTO.getPageSize());
        return examMapper.selectExamList(examQueryDTO);
    }
}
